using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class ChiTietCBayDA : DataAccessBase
    {
		#region Constructors

        public ChiTietCBayDA()
            : base()
        {
			_selectSql = "Select * From CHITIETCBAY";
			_insertSql = "Insert Into CHITIETCBAY(MACB,MAGHEVE,GIAVE,SLGHE,SLCONLAI) Values (@MACB,@MAGHEVE,@GIAVE,@SLGHE,@SLCONLAI)";
        	_updateSql = "Update CHITIETCBAY Set MACB=@MACB,MAGHEVE=@MAGHEVE,GIAVE=@GIAVE,SLGHE=@SLGHE,SLCONLAI=@SLCONLAI Where 1=1 And MACTCBAY=@MACTCBAY";
        	_deleteSql = "Delete From CHITIETCBAY Where 1=1 And MACTCBAY=@MACTCBAY";
        	_primarSql = "Select * From CHITIETCBAY Where 1=1 And MACTCBAY=@MACTCBAY";
        }

        protected static ChiTietCBayDA _inst = null;
        public static ChiTietCBayDA Inst
		{
			get
			{
				if(_inst == null)
                    _inst = new ChiTietCBayDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private ChiTietChuyenBay GetCurrent(IDataReader reader, bool isDeep)
		{
			ChiTietChuyenBay ret = new ChiTietChuyenBay();
            ret.MaCTCBay = (int)reader["MACTCBAY"];
            ret.GiaVe = (decimal)reader["GIAVE"];
            ret.SLGhe = (int)reader["SLGHE"];
            ret.SLGheConLai = (int)reader["SLCONLAI"];
			
			ret.ItemChuyenBay = new ChuyenBay();
			if(isDeep)
			{
                ret.ItemChuyenBay = ChuyenBayDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MACB"]);
			}
			else
                ret.ItemChuyenBay.MaChuyenBay = (string)reader["MACB"];
			
			ret.ItemCTGheVe = new Ve_Ghe();
			if(isDeep)
			{
                ret.ItemCTGheVe = VeGheDA.Inst.GetByPrimaryKey(isDeep, (int)reader["MAGHEVE"]);
			}
			else
                ret.ItemCTGheVe.MaGheVe = (int)reader["MAGHEVE"];
			
			
			return ret;
		}

		public List<ChiTietChuyenBay> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ChiTietChuyenBay> ret = new List<ChiTietChuyenBay>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
        public List<ChiTietChuyenBay> GetListTTVeByMaCB(string MaCB)
        {
            var strSQL = "select * from CHITIETCBAY ct where MACB=@MaCB";
            DbCommand comm = GetCommand(strSQL);
            SetParam(comm, "MaCB", MaCB);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ChiTietChuyenBay> ret = new List<ChiTietChuyenBay>();
                    while (dr.Read())
                    {
                        var item = GetCurrent(dr, false);
                        item.ItemCTGheVe = VeGheDA.Inst.GetByPrimaryKey(true, item.ItemCTGheVe.MaGheVe);
                        ret.Add(item);
                    }
                    return ret;
                }
            }
        }
        public List<ChiTietChuyenBay> GetChuyenBayGiaThap(bool isDeep)
        {
            var str = "select top 8 ct.* from CHITIETCBAY ct ,CHUYENBAY cb where ct.MACB=cb.MACB and cb.NGAYBAY>=convert(date,GETDATE()) order by ct.GIAVE";
            DbCommand comm = GetCommand(str);            
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ChiTietChuyenBay> ret = new List<ChiTietChuyenBay>();
                    while (dr.Read())
                    {
                        var item = GetCurrent(dr, true);
                        item.ItemCTGheVe = VeGheDA.Inst.GetByPrimaryKey(true, item.ItemCTGheVe.MaGheVe);
                        ret.Add(item);
                    }
                    return ret;
                }
            }
        }
		public ChiTietChuyenBay GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MACTCBAY", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(ChiTietChuyenBay entity)
		{

			DbCommand cmd = GetCommand(_insertSql);           

            SetParam(cmd, "@GIAVE", entity.GiaVe);
            SetParam(cmd, "@SLGHE", entity.SLGhe);
            SetParam(cmd, "@SLCONLAI", entity.SLGheConLai);

			SetParam(cmd, "@MACB", entity.ItemChuyenBay.MaChuyenBay);
			SetParam(cmd, "@MAGHEVE", entity.ItemCTGheVe.MaGheVe);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(ChiTietChuyenBay entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MACTCBAY", entity.MaCTCBay);
            SetParam(cmd, "@GIAVE", entity.GiaVe);
            SetParam(cmd, "@SLGHE", entity.SLGhe);
            SetParam(cmd, "@SLCONLAI", entity.SLGheConLai);

			SetParam(cmd, "@MACB", entity.ItemChuyenBay.MaChuyenBay);
			SetParam(cmd, "@MAGHEVE", entity.ItemCTGheVe.MaGheVe);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(ChiTietChuyenBay entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MACTCBAY", entity.MaCTCBay);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


